package info.niwota.webime.http;

/**
 *  HTTP Web Server
 *  @author qiangli
 */


public class HttpServer implements Lifecycle {
	private boolean running = false;
	
	private HttpConnector conn;
	
	public HttpServer() {
	}

	synchronized public void init(ServerContext ctx) {
		//
		conn = new HttpConnector();
		conn.init(ctx);
		
	}

	public int getPort() {
		return (conn == null ? -1 : conn.getPort());
	}
	
	public boolean isRunning() {
		return running;
	}

	synchronized public void startup() {
		if (running) {
			try {
				shutdown();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		running = true;
		//
		HttpAdapter pi = new HttpAdapter();
		//
		pi.addHandler(new WebInputHandler());
		//
		try {
			conn.setPI(pi);
			conn.startup();
		} catch (Exception e) {
			running = false;
			//
			e.printStackTrace();
		}
	}

	synchronized public void shutdown() {
		try {
			conn.shutdown();
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		running = false;
	}

}
